package quercus;

import java.util.ArrayList;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;

/**
 * Script engine factory
 */
public class MondayScriptingFactory implements ScriptEngineFactory {

	/**
	 * 
	 */
	@SuppressWarnings("unused")
	private static final long serialVersionUID = 4963537030946701446L;

	/**
	 * Returns the full name of the ScriptEngine.
	 */
	public String getEngineName() {
		return "Caucho Quercus Script Engine";
	}

	/**
	 * Returns the version of the ScriptEngine.
	 */
	public String getEngineVersion() {

		return "MondayScriptingEngine";
	}

	/**
	 * Returns an array of filename extensions normally used by this language.
	 */
	public List<String> getExtensions() {
		ArrayList<String> ext = new ArrayList<String>();
		ext.add("phpt");
		return ext;
	}

	/**
	 * Returns the mime-types for scripts for the engine.
	 */
	public List<String> getMimeTypes() {
		return new ArrayList<String>();
	}

	/**
	 * Returns the short names for the scripts for the engine, e.g.
	 * {"javascript", "rhino"}
	 */
	public List<String> getNames() {
		ArrayList<String> names = new ArrayList<String>();
		names.add("monday");
		names.add("phpt");
		return names;
	}

	/**
	 * Returns the name of the supported language.
	 */
	public String getLanguageName() {
		return "php";
	}

	/**
	 * Returns the version of the scripting language.
	 */
	public String getLanguageVersion() {
		return "5.3.2";
	}

	/**
	 * Returns engine-specific properties.
	 * 
	 * Predefined keys include:
	 * <ul>
	 * <li>THREADING
	 * </ul>
	 */
	public Object getParameter(String key) {
		if ("THREADING".equals(key))
			return "THREAD-ISOLATED";
		else if (ScriptEngine.ENGINE.equals(key))
			return getEngineName();
		else if (ScriptEngine.ENGINE_VERSION.equals(key))
			return getEngineVersion();
		else if (ScriptEngine.NAME.equals(key))
			return getEngineName();
		else if (ScriptEngine.LANGUAGE.equals(key))
			return getLanguageName();
		else if (ScriptEngine.LANGUAGE_VERSION.equals(key))
			return getLanguageVersion();
		else
			return null;
	}

	/**
	 * Returns a string which could invoke a method of a Java object.
	 */
	public String getMethodCallSyntax(String obj, String m, String[] args) {
		StringBuilder sb = new StringBuilder();
		sb.append("$");
		sb.append(obj);
		sb.append("->");
		sb.append(m);
		sb.append("(");
		for (int i = 0; i < args.length; i++) {
			if (i != 0)
				sb.append(", ");
			sb.append("$");
			sb.append(args[i]);
		}
		sb.append(");");
		return sb.toString();
	}

	/**
	 * Returns a string which generates an output statement.
	 */
	public String getOutputStatement(String toDisplay) {
		return "echo(\'" + toDisplay.replace("\'", "\\\'") + "\');";
	}

	/**
	 * Returns a string which generates a valid program.
	 */
	public String getProgram(String[] statements) {
		StringBuilder sb = new StringBuilder();
		sb.append("<?php\n");
		for (int i = 0; i < statements.length; i++) {
			sb.append(statements[i]);
			sb.append(";\n");
		}
		sb.append("?>\n");
		return sb.toString();
	}

	/**
	 * Returns a ScriptEngine instance.
	 */
	public MondayScriptingEngine getScriptEngine() {
		return new MondayScriptingEngine(this);
	}

	public String toString() {
		return "MondayScriptEngineFactory[]";
	}
}
